'use strict';

angular.module('SystemManage').controller('RoleController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'PublicEnum', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, PublicEnum, $location, Authentication) {
        //$scope.authentication = Authentication;
       
        $scope.btnSave = function(){
            if($scope.Module.Role.FocusedRecord.Code == undefined || 
                $scope.Module.Role.FocusedRecord.Code.trim() == ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Role.FocusedRecord.Name == undefined || 
                $scope.Module.Role.FocusedRecord.Name.trim() == ""){
                PublicFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            var menus = [];
            for(var i=0; i<$scope.Module._DisplayMenu.Records.length; i++){
                if($scope.Module._DisplayMenu.Records[i].MenuId == undefined || 
                    $scope.Module._DisplayMenu.Records[i].MenuId == null || 
                    $scope.Module._DisplayMenu.Records[i].MenuId == ""){
                    PublicFunction.alert("菜单不能为空！", "保存错误");
                    return;
                }
                for(var j=0; j<menus.length; j++){
                    if(menus[j] == $scope.Module._DisplayMenu.Records[i].MenuId){
                        PublicFunction.alert("菜单不能重复！", "保存错误");
                        return;
                    }
                }
                menus.push($scope.Module._DisplayMenu.Records[i].MenuId);
            }

            $scope.Module.Role.FocusedRecord.Menus = menus;
            if($scope.Module.Role.FocusedRecord.RowStatus == PublicEnum.RowStatus.UnChange){
                $scope.Module.Role.FocusedRecord.RowStatus = PublicEnum.RowStatus.Modify;
            }
            $scope.Module.Role.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.Role.cancel(loadMenu);            
        };
        $scope.btnAdd = function(){
            $scope.Module.Role.add();
            loadMenu();
        };
        $scope.btnDelete = function(){
            $scope.Module.Role.delete(loadMenu);
        };
        $scope.btnList = function(){
            $location.url('/index/Role/List');
        };
        $scope.btnFirstPage = function(){
            $scope.Module.Role.firstPage(loadMenu);
        };
        $scope.btnPrePage = function(){
            $scope.Module.Role.prePage(loadMenu);
        };
        $scope.btnNextPage = function(){
            $scope.Module.Role.nextPage(loadMenu);
        };
        $scope.btnLastPage = function(){
            $scope.Module.Role.lastPage(loadMenu);
        };
        $scope.btnFindChange = function(){
            loadMenu();
        };
        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName == "MenuCode"){
                if(newValue.length > 0){
                    $scope.Module._DisplayMenu.FocusedRecord.ParentName = newValue[0].Parent.Name;
                    $scope.Module._DisplayMenu.FocusedRecord.URL = newValue[0].URL;
                }
                else{
                    $scope.Module._DisplayMenu.FocusedRecord.ParentName = "";
                    $scope.Module._DisplayMenu.FocusedRecord.URL = "";
                }
                $scope.Module._DisplayMenu.refreshDataGridRows();
            }
        };
        $scope.refBeforeShow = function(refScope){
            var menus = [];
            for(var i=0; i<$scope.Module._DisplayMenu.Records.length; i++){
                if($scope.Module._DisplayMenu.Records[i].MenuId != undefined && $scope.Module._DisplayMenu.Records[i].MenuId != ""){
                    menus.push($scope.Module._DisplayMenu.Records[i].MenuId);
                }
            }
            refScope.Module.QueryParameter.Condition = {Parent: {$ne: null}, _id: {$nin: menus}};
        }

        var setMenus = function(menuReords){
            $scope.Module._DisplayMenu.Records = [];
            for(var i=0; i<menuReords.length; i++){
                var newRecord = $scope.Module._DisplayMenu.add();
                newRecord.MenuId = menuReords[i]._id;
                newRecord.MenuCode = menuReords[i].Code;
                newRecord.MenuName = menuReords[i].Name;
                newRecord.URL = menuReords[i].URL;
                if(menuReords[i].Parent != undefined && menuReords[i].Parent != null){
                    newRecord.ParentName = menuReords[i].Parent.Name;
                }
            }
            $scope.Module._DisplayMenu.firstPage();
        };
        var loadMenu = function(){
            //$scope.Module.Menu.QueryParameter.Condition = ;
            if($scope.Module.Role.FocusedRecord.Menus != undefined && $scope.Module.Role.FocusedRecord.Menus != null){
                $scope.Module.Menu.find({_id: {$in: $scope.Module.Role.FocusedRecord.Menus}}, null, function(menuReords){
                    setMenus(menuReords);
                });
            }
            else{
                setMenus([]);
            }
        };

        //初始化
        PublicModule.initModule($scope, 'Role');
        //$scope.Module.Role.QueryParameter.Condition = ;
        PublicModule.initModule($scope, 'Menu');
        $scope.Module.Menu.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.Menu.QueryParameter.Populate = {path: 'Parent', select: 'Name'};


        PublicModule.initModule($scope, '_DisplayMenu');
        $scope.Module._DisplayMenu.PageSize = 10;
        $scope.Module._DisplayMenu.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;

        if($stateParams.id) {
            $scope.Module.Role.findById($stateParams.id, function(record){
                $scope.Module.Role.FocusedRecord = record;
                loadMenu();
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
